package com.example.lanouhn.bantangapp.activities.activities;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lanouhn.bantangapp.R;

/**
 *  community 文章的 item-->评论
 * Created by lanouhn on 16/8/2.
 */
public class Details_Comment_Activity extends Activity {

    private ImageView back;//返回
    private TextView tv_sendout;//发送
    private ProgressBar progressbar;//转转转
    private EditText et_comment_content;//内容
    private TextView tv_zi_shu;// 字数

    private CharSequence temp;
    private int editStart ;
    private int editEnd ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.details_comment_activity);
        //初始化
        initView();
        //监听事件
        initListener();
    }
    //初始化
    private void initView() {
        back = (ImageView) findViewById(R.id.back);
        tv_sendout = (TextView) findViewById(R.id.tv_sendout);
        progressbar = (ProgressBar) findViewById(R.id.progressbar);
        et_comment_content = (EditText) findViewById(R.id.et_comment_content);
        tv_zi_shu = (TextView) findViewById(R.id.tv_zi_shu);


    }


    //监听事件
    private void initListener() {
        //返回
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //内容
        et_comment_content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                temp = s;
                

                if (et_comment_content.getText().toString().trim().length() > 0) {
                    tv_sendout.setTextColor(getResources().getColor(R.color.colorAccent));
                    tv_sendout.setClickable(true);//设置完成按钮可以点击



                } else {
                    tv_sendout.setTextColor(getResources().getColor(R.color.blank_gray));
                    tv_sendout.setClickable(false);//设置完成按钮不能点击
                }


            }

            @Override
            public void afterTextChanged(Editable s) {
               //发送评论成功 获得 5 积分的消息
                tv_sendout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(Details_Comment_Activity.this, R.string.article_item_e, Toast.LENGTH_SHORT).show();
                        progressbar.setVisibility(View.VISIBLE);
                        finish();
                    }
                });



                editStart = et_comment_content.getSelectionStart();
                editEnd = et_comment_content.getSelectionEnd();
                tv_zi_shu.setText(temp.length()+"" );
                if (temp.length() > 140) {
                    Toast.makeText(Details_Comment_Activity.this,
                            R.string.word, Toast.LENGTH_SHORT)
                            .show();
                    s.delete(editStart-1, editEnd);
                    int tempSelection = editStart;
                    et_comment_content.setText(s);
                    et_comment_content.setSelection(tempSelection);
                }

            }

        });




    }
}
